package topin.facecat.FaceCat.UI

import facecat.topin.core.FCColor
import facecat.topin.core.FCDockStyle
import facecat.topin.core.FCInvokeEventCallBack
import facecat.topin.core.FCLayoutStyle
import facecat.topin.core.FCNative
import facecat.topin.core.FCPaint
import facecat.topin.core.FCRect
import facecat.topin.core.FCSize
import facecat.topin.core.FCTouchEventCallBack
import facecat.topin.core.FCTouchInfo
import facecat.topin.core.FCTran
import facecat.topin.core.FCView
import facecat.topin.core.RefObject
import facecat.topin.div.FCDiv
import facecat.topin.div.FCLayoutDiv
import facecat.topin.div.FCSplitLayoutDiv
import facecat.topin.service.FCClientService
import facecat.topin.service.FCListenerMessageCallBack
import facecat.topin.service.FCMessage
import topin.facecat.FaceCat.Service.HistoryServiceClient
import topin.facecat.FaceCat.Service.Security
import topin.facecat.FaceCat.Service.SecurityData
import topin.facecat.FaceCat.Service.SecurityLatestData
import topin.facecat.FaceCat.Service.StockService
import topin.facecat.FaceCat.Service.StockService.ConvertXDR
import topin.facecat.FaceCat.Service.StockService.ConvertXDRAfterPrice
import topin.facecat.FaceCat.Service.StockService.getHistoryHalfYearDatas
import topin.facecat.FaceCat.Service.StockService.getHistoryMonthDatas
import topin.facecat.FaceCat.Service.StockService.getHistorySeasonDatas
import topin.facecat.FaceCat.Service.StockService.getHistoryWeekDatas
import topin.facecat.FaceCat.Service.StockService.getHistoryYearDatas
import topin.facecat.FaceCat.Service.StockService.mergeLatestData
import topin.facecat.FaceCat.Service.StockService.multiMinuteSecurityDatas


class MyChart : FCSplitLayoutDiv(), FCTouchEventCallBack, FCListenerMessageCallBack,
    FCInvokeEventCallBack {
    var m_chart: FCChartEx? = null
    var m_code = "600000.SH"
    var m_bindCode = ""
    var m_subscription = 0
    var m_cycle = -1
    fun onSearchCode(code: String) {
        m_code = code
        if (m_cycle > 0) {
            m_lastPixel = m_chart!!.m_hScalePixel
        }
        searchSecurity(code, m_cycle)
    }

    private val m_clientTickDataCache = ClientTickDataCache()

    private val m_securityDatasCache = ArrayList<SecurityData>()

    /*
     * 回调方法
     *@param sender
     *@param message
     *@param invoke
     */
    override fun callListenerMessageEvent(sender: Any, message: FCMessage, invoke: Any) {
        m_chart!!.invoke(message)
    }

    var m_requestID = -1
    var m_lastChangeID = 0
    override fun onPaint(paint: FCPaint, clipRect: FCRect) {
        if (m_lastChangeID != FCNative.getChangeID()) {
            m_lastChangeID = FCNative.getChangeID()
            if (MyColor.m_style == 0) {
                chartToBlack(m_chart)
            } else {
                chartToWhite(m_chart)
            }
        }
        super.onPaint(paint, clipRect)
    }

    var m_lastPixel = 11.0

    /*
     * 绑定历史数据
     *@param chart
     *@param historyDatas
     */
    fun bindHistoryDatas(
        code: String,
        cycle: Int,
        historyDatas: ArrayList<SecurityData>?,
        refresh: Boolean
    ) {
        if (code == m_code && m_cycle == cycle) {
            m_bindCode = code
            if (MyColor.m_style == 0) {
                chartToBlack(m_chart)
            } else {
                chartToWhite(m_chart)
            }
            if (cycle == 0) {
                m_chart!!.m_upColor = MyColor.USERCOLOR168
                m_chart!!.m_downColor = MyColor.USERCOLOR169
            }
            m_chart!!.m_datas!!.clear()
            m_chart!!.m_shapes.clear()
            m_chart!!.resetChartVisibleRecord()
            m_chart!!.m_lastValidIndex = -1
            m_chart!!.checkChartLastVisibleIndex()
            m_chart!!.m_candlePaddingTop = 30
            m_chart!!.m_candlePaddingBottom = 10
            if (m_cycle == 0) {
                m_chart!!.m_cycle = "trend"
                m_chart!!.m_hScaleFormat = "HH:mm"
                m_chart!!.m_autoFillHScale = true
                m_chart!!.m_magnitude = 100
                m_chart!!.m_rightSpace = 0
                m_chart!!.m_leftVScaleWidth = 60
            } else if (m_cycle < 1440) {
                m_chart!!.m_cycle = "minute"
                m_chart!!.m_hScaleFormat = "HH:mm"
                m_chart!!.m_autoFillHScale = false
                m_chart!!.m_magnitude = 10000
                m_chart!!.m_hScalePixel = m_lastPixel
                m_lastPixel = m_chart!!.m_hScalePixel
                m_chart!!.m_rightSpace = 20
                m_chart!!.m_leftVScaleWidth = 0
            } else {
                m_chart!!.m_cycle = "day"
                m_chart!!.m_hScaleFormat = "yyyy-MM-dd"
                m_chart!!.m_autoFillHScale = false
                m_chart!!.m_magnitude = 10000
                m_chart!!.m_hScalePixel = m_lastPixel
                m_lastPixel = m_chart!!.m_hScalePixel
                m_chart!!.m_rightSpace = 20
                m_chart!!.m_leftVScaleWidth = 0
            }
            val security = Security()
            if (DataCenter.m_securityService!!.getSecurityByCode(m_code, security)) {
                val showCode = security.m_code.substring(0, security.m_code.indexOf("."))
                val sName = showCode + " " + security.m_name
                m_chart!!.m_text = sName
            }
            val ary = ArrayList<SecurityData>()
            for (i in historyDatas!!.indices) {
                ary.add(historyDatas[i])
            }
            m_chart!!.m_datas = ary
            if (cycle == 0 && m_chart!!.m_datas!!.size > 0) {
                m_chart!!.m_firstOpen = m_chart!!.m_datas!![m_chart!!.m_datas!!.size - 1].m_close
            }
            addAvgLine()
            m_chart!!.resetChartVisibleRecord()
            m_chart!!.checkChartLastVisibleIndex()
            m_chart!!.calcChartIndicator()
            if (MainFrame.m_mainFrame.m_latestData != null && MainFrame.m_mainFrame.m_latestData!!.m_requestID == MainFrame.m_mainFrame.m_requestID1) {
//                   lock (m_securityDatasCache)
                run {
                    m_securityDatasCache.clear()
                    for (i in historyDatas.indices) {
                        m_securityDatasCache.add(historyDatas[i])
                    }
                    m_clientTickDataCache.m_code = m_code
                    m_clientTickDataCache.m_lastAmount =
                        MainFrame.m_mainFrame.m_latestData!!.m_amount
                    m_clientTickDataCache.m_lastDate = MainFrame.m_mainFrame.m_latestData!!.m_date
                    m_clientTickDataCache.m_lastVolume =
                        MainFrame.m_mainFrame.m_latestData!!.m_volume
                }
                newData(
                    MainFrame.m_mainFrame.m_latestData,
                    MainFrame.m_mainFrame.m_latestData!!.m_requestID,
                    false
                )
            }
            if (refresh) {
                invalidate()
            }
        }
    }

    /*
     * 添加均价线
     */
    fun addAvgLine() {
        if (m_cycle == 0) {
            val security = Security()
            if (DataCenter.m_securityService!!.getSecurityByCode(m_code, security)) {
                var validIndex = -1
                m_chart!!.m_shapes.clear()
                var shape: BaseShape? = null
                if (security.m_type > 0) {
                    shape = BaseShape()
                    shape.m_color = MyColor.USERCOLOR200
                    shape.m_shapeName = "avg"
                    m_chart!!.m_shapes.add(shape)
                }
                for (i in m_chart!!.m_datas!!.indices) {
                    if (shape != null) {
                        var avgValue = 0.0
                        var sumVolume = 0.0
                        var sumPrice = 0.0
                        for (j in 0..i) {
                            sumVolume += m_chart!!.m_datas!![j].m_volume
                            sumPrice += m_chart!!.m_datas!![j].m_amount
                        }
                        if (sumVolume > 0) {
                            avgValue = sumPrice / sumVolume
                        }else{
                            avgValue = m_chart!!.m_datas!![i].m_close
                        }
                        shape.m_datas.add(avgValue)
                    }
                    if (m_chart!!.m_datas!![i].m_volume > 0) {
                        validIndex = i
                    }
                }
                m_chart!!.m_lastValidIndex = validIndex
            }
        }
    }

    /*
     * 添加组件方法
     */
    override fun onAdd() {
        super.onAdd()
        if (m_chart == null) {
            val topDiv = FCSplitLayoutDiv()
            topDiv.size = FCSize(400, 30)
            topDiv.backColor = FCColor.None
            topDiv.borderColor = FCColor.None
            addView(topDiv)
            val bottomDiv = FCDiv()
            bottomDiv.backColor = FCColor.None
            bottomDiv.borderColor = FCColor.None
            addView(bottomDiv)
            splitter.bounds = FCRect(0, 30, width, 31)
            firstView = topDiv
            secondView = bottomDiv
            m_chart = FCChartEx()
            m_chart!!.m_myChart = this
            bottomDiv.addView(m_chart)
            m_chart!!.borderColor = FCColor.None
            m_chart!!.dock = FCDockStyle.Fill
            m_chart!!.m_cycle = "day"
            m_chart!!.m_allowDragChartDiv = true
            m_chart!!.borderColor = FCColor.None
            m_chart!!.m_crossTipColor = MyColor.USERCOLOR219
            m_chart!!.m_leftVScaleWidth = 0
            m_chart!!.m_rightVScaleWidth = 60
            m_chart!!.m_candlePaddingTop = 30
            m_chart!!.m_hScaleHeight = 25
            m_chart!!.m_hScaleTextDistance = 8
            m_chart!!.m_candlePaddingBottom = 10
            if (MyColor.m_style == 0) {
                chartToBlack(m_chart)
            } else {
                chartToWhite(m_chart)
            }
            m_chart!!.m_candleDivPercent = 0.7
            m_chart!!.m_volDivPercent = 0.3
            m_chart!!.m_indDivPercent = 0.0
            m_chart!!.m_candleStyle = "rect2"
            m_chart!!.m_barStyle = "rect2"
            topDiv.layoutStyle = FCLayoutStyle.RightToLeft
            val rLayout = FCLayoutDiv()
            rLayout.borderColor = FCColor.None
            rLayout.backColor = FCColor.None
            topDiv.addView(rLayout)
            val lLayout = FCLayoutDiv()
            lLayout.borderColor = FCColor.None
            lLayout.backColor = FCColor.None
            lLayout.setAllowPreviewsEvent(true)
            lLayout.setAllowDragScroll(true)
            lLayout.setShowHScrollBar(true)
            topDiv.addView(lLayout)
            lLayout.hScrollBar.height = 0
            topDiv.splitter.bounds = FCRect(topDiv.width, 0, topDiv.width, topDiv.height)
            topDiv.splitter.borderColor = FCColor.None
            topDiv.firstView = rLayout
            topDiv.secondView = lLayout
            val cycles = ArrayList<String>()
            cycles.add("1")
            cycles.add("5")
            cycles.add("10")
            cycles.add("15")
            cycles.add("20")
            cycles.add("30")
            cycles.add("60")
            cycles.add("90")
            cycles.add("120")
            cycles.add("日")
            cycles.add("周")
            cycles.add("月")
            cycles.add("季")
            cycles.add("半")
            cycles.add("年")
            val cyclesInts = ArrayList<Int>()
            cyclesInts.add(1)
            cyclesInts.add(5)
            cyclesInts.add(10)
            cyclesInts.add(15)
            cyclesInts.add(20)
            cyclesInts.add(30)
            cyclesInts.add(60)
            cyclesInts.add(90)
            cyclesInts.add(120)
            cyclesInts.add(1440)
            cyclesInts.add(10080)
            cyclesInts.add(43200)
            cyclesInts.add(129600)
            cyclesInts.add(259200)
            cyclesInts.add(518400)
            for (i in cycles.indices) {
                val btnCycle = CycleButton()
                btnCycle.borderColor = FCColor.None
                btnCycle.size = FCSize(27, 30)
                btnCycle.text = cycles[i]
                btnCycle.m_cycle = cyclesInts[i]
                btnCycle.m_chart = this
                btnCycle.name = cyclesInts[i].toString()
                lLayout.addView(btnCycle)
                btnCycle.addEvent(this, "onclick", this)
                m_cycleButtons.add(btnCycle)
            }
            m_chart!!.addEvent(this, "oninvoke", this)
        }
    }

    var m_cycleButtons = ArrayList<CycleButton>()
    override fun delete() {
        if (!isDeleted) {
            DataCenter.m_historyServiceClient!!.removeListener(m_requestID)
        }
        super.delete()
    }

    /*
     * 查询代码
     *@param code
     *@param cycle
     */
    fun searchSecurity(code: String, cycle: Int) {
        m_chart!!.m_floatStrs.clear()
        m_subscription = m_chart!!.subScription
        m_code = code
        val oldReqID = m_requestID
        m_requestID = FCClientService.getRequestID()
        if (oldReqID != -1) {
            DataCenter.m_historyServiceClient!!.removeListener(oldReqID)
            //DataCenter.m_historyServiceClient.stopData(oldReqID);
        }
        DataCenter.m_historyServiceClient!!.addListener(m_requestID, this, this)
        var realCycle = m_cycle
        if (realCycle > 0) {
            realCycle = if (realCycle < 1440) {
                1
            } else {
                1440
            }
        }
        DataCenter.m_historyServiceClient!!.reqData(m_requestID, m_code, realCycle, 0, 0.0, 0.0)
        if (cycle == 0) {
            m_chart!!.m_subComboBox!!.isVisible = false
        } else {
            val security = Security()
            if (DataCenter.m_securityService!!.getSecurityByCode(m_code, security)) {
                if (security.m_type > 0) {
                    m_chart!!.m_subComboBox!!.isVisible = true
                } else {
                    m_chart!!.m_subComboBox!!.isVisible = false
                }
            } else {
                m_chart!!.m_subComboBox!!.isVisible = false
            }
        }
    }

    override fun update() {
        if (m_cycleButtons.size > 0) {
            val dWidth = width - m_chart!!.m_rightVScaleWidth
            var cWidth = dWidth / m_cycleButtons.size
            var addWidth = 0
            if (cWidth < 27) {
                cWidth = 27
            } else if (cWidth > 35) {
                cWidth = 35
            } else {
                addWidth = dWidth - cWidth * m_cycleButtons.size
            }
            for (i in m_cycleButtons.indices) {
                m_cycleButtons[i].width = cWidth
            }
            if (addWidth > 0) {
                for (i in m_cycleButtons.indices.reversed()) {
                    if (addWidth > 0) {
                        m_cycleButtons[i].width = m_cycleButtons[i].width + 1
                        addWidth--
                    }
                }
            }
        }
        super.update()
    }

    /*
     * 设置属性
     *@param name
     *@param value
     */
    override fun setAttribute(name: String, value: String) {
        if (name == "cycle") {
            m_cycle = FCTran.strToInt(value)
        } else if (name == "candledivpercent") {
            m_chart!!.m_candleDivPercent = FCTran.strToDouble(value)
        } else if (name == "voldivpercent") {
            m_chart!!.m_volDivPercent = FCTran.strToDouble(value)
        } else {
            super.setAttribute(name, value)
        }
    }

    /*
     * 点击事件
     *@param evenName
     *@param sender
     *@param touchInfo
     *@param invoke
     */
    override fun callTouchEvent(
        eventName: String,
        sender: Any,
        touchInfo: FCTouchInfo,
        invoke: Any
    ) {
        super.callTouchEvent(eventName, sender, touchInfo, invoke)
        val view = sender as? FCView
        if (view is CycleButton) {
            if (m_cycle == 0) {
                m_chart!!.m_hScalePixel = 11.0
            } else {
                m_lastPixel = m_chart!!.m_hScalePixel
            }
            val view1 = (if (view is CycleButton) view else null)
            m_cycle = FCTran.strToInt(view.getName())
            view.getParent().invalidate()
            searchSecurity(m_code, m_cycle)
            m_chart!!.focus()
        }
    }

    /*
     * 图表的黑色风格
     *@param chart
     */
    fun chartToBlack(chart: FCChart?) {
        chart!!.m_scaleColor = FCColor.rgb(75, 75, 75)
        chart.m_crossLineColor = FCColor.rgb(100, 100, 100)
        chart.m_gridColor = FCColor.rgb(50, 50, 50)
        chart.m_upColor = FCColor.rgb(186, 56, 18)
        chart.m_downColor = FCColor.rgb(31, 182, 177)
        if (m_cycle == 0) {
            chart.m_downColor = FCColor.rgb(15, 193, 118)
        }
        chart.m_trendColor = FCColor.rgb(255, 255, 255)
        val indicatorColors = ArrayList<Long>()
        chart.m_indicatorColors = indicatorColors
        indicatorColors.add(FCColor.rgb(255, 255, 255))
        indicatorColors.add(FCColor.rgb(255, 255, 0))
        indicatorColors.add(FCColor.rgb(255, 0, 255))
        indicatorColors.add(FCColor.rgb(255, 0, 0))
        indicatorColors.add(FCColor.rgb(0, 255, 255))
        indicatorColors.add(FCColor.rgb(0, 255, 0))
        indicatorColors.add(FCColor.rgb(255, 255, 0))
        indicatorColors.add(FCColor.rgb(255, 255, 255))
        chart.m_candleStyle = "rect2"
    }

    /*
     * 白色风格
     *@param chart
     */
    fun chartToWhite(chart: FCChart?) {
        chart!!.m_scaleColor = FCColor.rgb(150, 150, 150)
        chart.m_crossLineColor = FCColor.rgb(150, 150, 150)
        chart.m_gridColor = FCColor.rgba(150, 150, 150, 125)
        chart.m_upColor = FCColor.rgb(255, 80, 80)
        chart.m_downColor = MyColor.USERCOLOR169
        if (m_cycle == 0) {
            chart.m_downColor = FCColor.rgb(15, 193, 118)
        }
        chart.m_trendColor = FCColor.rgb(0, 0, 0)
        val indicatorColors = ArrayList<Long>()
        chart.m_indicatorColors = indicatorColors
        indicatorColors.add(FCColor.rgb(50, 50, 50))
        indicatorColors.add(FCColor.rgb(50, 50, 50))
        indicatorColors.add(FCColor.rgb(50, 50, 50))
        indicatorColors.add(FCColor.rgb(50, 50, 50))
        indicatorColors.add(FCColor.rgb(50, 50, 50))
        indicatorColors.add(FCColor.rgb(50, 50, 50))
        indicatorColors.add(FCColor.rgb(100, 100, 100))
        indicatorColors.add(FCColor.rgb(50, 50, 50))
        chart.m_candleStyle = "rect"
    }

    var m_clearRequestID = -1

    init {
        backColor = FCColor.None
        borderColor = FCColor.None
        layoutStyle = FCLayoutStyle.TopToBottom
        size = FCSize(400, 400)
        MainFrame.m_charts.add(this)
    }

    /*
     * 接收最新数据
     *@param latestData
     */
    fun newData(latestData: SecurityLatestData?, requestID: Int, refresh: Boolean) {
        if (latestData!!.m_code != m_bindCode) {
            return
        }
        //           lock (m_securityDatasCache)
        run {
            if (m_cycle == 0) {
                m_chart!!.m_firstOpen = latestData.m_lastClose
            }
            //集合竞价处理
            if (m_cycle == 0 && latestData.m_volume == 0.0) {
                m_clearRequestID = requestID
                for (i in m_chart!!.m_datas!!.indices) {
                    val sData = m_chart!!.m_datas!![i]
                    sData.m_open = 0.0
                    sData.m_close = 0.0
                    sData.m_high = 0.0
                    sData.m_low = 0.0
                    sData.m_volume = 0.0
                    sData.m_amount = 0.0
                }
                m_chart!!.m_shapes.clear()
                m_chart!!.calculateChartMaxMin()
                m_chart!!.checkChartLastVisibleIndex()
                if (refresh) {
                    m_chart!!.invalidate()
                }
            } else if (m_clearRequestID == requestID) {
                searchSecurity(latestData.m_code, 0)
                m_clearRequestID = -1
            } else {
                val newData = SecurityData()
                newData.m_amount = latestData.m_amount
                newData.m_volume = latestData.m_volume
                newData.m_high = latestData.m_high
                newData.m_low = latestData.m_low
                newData.m_close = latestData.m_close
                newData.m_open = latestData.m_open
                newData.m_date = latestData.m_date
                if (m_securityDatasCache.size > 0) {
                    //后复权
                    if (m_subscription == 2) {
                        if (StockService.m_factors.containsKey(latestData.m_code)) {
                            val factor =
                                StockService.m_factors[latestData.m_code]!!
                            val sData = SecurityData()
                            sData.m_date =
                                ((latestData.m_date / 86400).toInt() * 86400).toDouble()
                            sData.m_close = latestData.m_close
                            sData.m_high = latestData.m_high
                            sData.m_low = latestData.m_low
                            sData.m_open = latestData.m_open
                            ConvertXDRAfterPrice(
                                latestData.m_code,
                                sData,
                                sData.m_date,
                                factor
                            )
                            newData.m_close = sData.m_close
                            newData.m_high = sData.m_high
                            newData.m_low = sData.m_low
                            newData.m_open = sData.m_open
                        }
                    }
                }
                var lastRecordIsVisible = false
                if (m_chart!!.m_lastVisibleIndex == m_chart!!.m_datas!!.size - 1) {
                    lastRecordIsVisible = true
                }
                m_clientTickDataCache.m_code = latestData.m_code
                val oldDatasSize = m_securityDatasCache.size
                mergeLatestData(
                    latestData.m_code,
                    m_securityDatasCache,
                    newData,
                    m_clientTickDataCache,
                    m_cycle
                )
                if (m_securityDatasCache.size > 0) {
                    var startIndex = oldDatasSize - 1
                    if (startIndex < 0) {
                        startIndex = 0
                    }
                    if (m_cycle == 0) {
                        startIndex = 0
                    }
                    for (i in startIndex until m_securityDatasCache.size) {
                        val securityData = m_securityDatasCache[i]
                        if (securityData.m_open > 0 && securityData.m_volume > 0 && securityData.m_amount > 0) {
                            if (m_cycle == 0) {
                                if (i < m_chart!!.m_datas!!.size) {
                                    if (m_chart!!.m_datas!![i].m_date == securityData.m_date) {
                                        m_chart!!.m_datas!![i] = securityData
                                    }
                                }
                            } else {
                                insertData(m_chart, securityData)
                            }
                        }
                    }
                    m_chart!!.m_shapes.clear()
                    addAvgLine()
                    m_chart!!.calculateChartMaxMin()
                    m_chart!!.checkChartLastVisibleIndex()
                    if (lastRecordIsVisible && m_chart!!.m_lastVisibleIndex != m_chart!!.m_datas!!.size - 1) {
                        m_chart!!.m_lastRecordIsVisible = true
                        m_chart!!.m_lastVisibleIndex = m_chart!!.m_datas!!.size - 1
                        val workingAreaWidth = m_chart!!.chartWorkAreaWidth
                        val maxVisibleRecord = m_chart!!.getChartMaxVisibleCount(
                            m_chart!!.m_hScalePixel,
                            workingAreaWidth.toDouble()
                        )
                        m_chart!!.m_firstVisibleIndex =
                            m_chart!!.m_lastVisibleIndex - maxVisibleRecord + 1
                        if (m_chart!!.m_firstVisibleIndex < 0) {
                            m_chart!!.m_firstVisibleIndex = 0
                        }
                    }
                    if (refresh) {
                        m_chart!!.invalidate()
                    }
                }
            }
        }
    }

    override fun callInvokeEvent(eventName: String, sender: Any, args: Any, invoke: Any) {
        if (args != null) {
            val message = args as? FCMessage
            if (message != null) {
                if (message.m_serviceID == HistoryServiceClient.SERVICEID) {
                    if (message.m_functionID == HistoryServiceClient.FUNCTIONID_GETDATA) {
                        if (message.m_requestID == m_requestID) {
                            var code = ""
                            var cycle = 0
                            val subscription = 0
                            var startDate = 0.0
                            var endDate = 0.0
                            var nowDate = 0.0
                            var nowVolume = 0.0
                            var nowAmount = 0.0
                            val Refcode = RefObject(code)
                            val Refcycle = RefObject(cycle)
                            val RefstartDate = RefObject(startDate)
                            val RefendDate = RefObject(endDate)
                            val Refsubscription = RefObject(subscription)
                            val RefnowDate = RefObject(nowDate)
                            val RefnowVolume = RefObject(nowVolume)
                            val RefnowAmount = RefObject(nowAmount)
                            var securityDatas = HistoryServiceClient.getDatas(
                                Refcode,
                                Refcycle,
                                RefstartDate,
                                RefendDate,
                                Refsubscription,
                                RefnowDate,
                                RefnowVolume,
                                RefnowAmount,
                                message.m_body,
                                message.m_bodyLength
                            )
                            code = Refcode.argvalue
                            cycle = Refcycle.argvalue
                            startDate = RefstartDate.argvalue
                            endDate = RefendDate.argvalue
                            nowDate = RefnowDate.argvalue
                            nowVolume = RefnowVolume.argvalue
                            nowAmount = RefnowAmount.argvalue
                            if (m_cycle > 0) {
                                if (m_subscription == 1) {
                                    ConvertXDR(code, 1, securityDatas!!)
                                } else if (m_subscription == 2) {
                                    ConvertXDR(code, 2, securityDatas!!)
                                }
                                if (m_cycle < 1440) {
                                    if (m_cycle > 1) {
                                        val newDatas = ArrayList<SecurityData>()
                                        multiMinuteSecurityDatas(
                                            newDatas,
                                            securityDatas!!, m_cycle
                                        )
                                        securityDatas = newDatas
                                    }
                                } else {
                                    if (m_cycle == 10080) {
                                        val newDatas = ArrayList<SecurityData>()
                                        getHistoryWeekDatas(
                                            newDatas,
                                            securityDatas!!
                                        )
                                        securityDatas = newDatas
                                    } else if (m_cycle == 43200) {
                                        val newDatas = ArrayList<SecurityData>()
                                        getHistoryMonthDatas(
                                            newDatas,
                                            securityDatas!!
                                        )
                                        securityDatas = newDatas
                                    } else if (m_cycle == 129600) {
                                        val newDatas = ArrayList<SecurityData>()
                                        getHistorySeasonDatas(
                                            newDatas,
                                            securityDatas!!
                                        )
                                        securityDatas = newDatas
                                    } else if (m_cycle == 259200) {
                                        val newDatas = ArrayList<SecurityData>()
                                        getHistoryHalfYearDatas(
                                            newDatas,
                                            securityDatas!!
                                        )
                                        securityDatas = newDatas
                                    } else if (m_cycle == 518400) {
                                        val newDatas = ArrayList<SecurityData>()
                                        getHistoryYearDatas(
                                            newDatas,
                                            securityDatas!!
                                        )
                                        securityDatas = newDatas
                                    }
                                }
                            }
                            bindHistoryDatas(code, m_cycle, securityDatas, true)
                        }
                    }
                }
            }
        }
    }

    fun clearDatas() {
        m_securityDatasCache.clear()
        m_chart!!.m_datas!!.clear()
        m_chart!!.m_shapes.clear()
        m_chart!!.invalidate()
    }

    companion object {
        /*
         * 插入数据
         *@param chart 证券视图
         *@param dataSource 数据源
         *@param fields 字段
         *@param securityData 证券数据
         */
        fun insertData(chart: FCChartEx?, securityData: SecurityData): Int {
            if (chart!!.m_datas!!.size > 0) {
                val date = securityData.m_date
                val lastDate = chart.m_datas!![chart.m_datas!!.size - 1].m_date
                if (date == lastDate) {
                    chart.m_datas!![chart.m_datas!!.size - 1] = securityData
                } else {
                    chart.m_datas!!.add(securityData)
                }
            } else {
                chart.m_datas!!.add(securityData)
            }
            return 1
        }
    }
}








